package com.ilikesoup.instant.monitor.sample.trigger;

import com.ilikesoup.instant.monitor.core.ChangeEventTrigger;
import com.ilikesoup.instant.monitor.core.Invocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class MethodWriteTrigger implements ChangeEventTrigger {

    @Override
    public Object copyOldValue(Invocation invocation) {
        return invocation.serialize(invocation.getTarget());
    }

    @Override
    public void fireChange(Invocation invocation, Object oldValue, PropertyChangeSupport propertyChangeSupport) {
        propertyChangeSupport.firePropertyChange(invocation.getMethodName(), oldValue, invocation.getSerializedArgs());
    }

    public static class MethodWriteListener implements PropertyChangeListener {
        private static final Logger logger = LoggerFactory.getLogger(MethodWriteListener.class);
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            logger.debug("写方法调用监听，对象信息：{}，调用信息：[method:{}, oldVal:{}, {}Val:{}]",
                    evt.getSource(), evt.getPropertyName(), evt.getOldValue(), evt.getPropertyName(), evt.getNewValue());
        }
    }

}
